﻿package net.wangit.dev.version;

import net.wangit.Path;
import net.wangit.Unit;
import net.wangit.context.ActionContext;
import net.wangit.context.ActionContextHelper;
import net.wangit.support.UnitInterceptor;
import net.wangit.util.Checker;
import net.wangit.util.FileUtil;
import net.wangit.util.StringUtil;

public class InputUI implements UnitInterceptor {

	private static final String KEY = "net.wangit.dev.version.InputUI.path";
	private static final String FOLDER_ICON = "/domain/dev/o/folder.png";
	private static final String FILE_ICON = "/domain/dev/o/file.png";

	public InputUI() {
	}

	public String intercept(ActionContext ac, Unit unit) throws Exception {
		String path = ActionContextHelper.getRequest2Session(ac, "input.path",
				InputUI.KEY, Path.getVersionPath());
		path = FileUtil.formatPath((new StringBuilder(String.valueOf(path)))
				.append("/").toString());
		unit.getElement("dir").setCustom(getDir(path));
		unit.getElement("list").setCustom(getList(path));
		unit.getElement("path").setDefaultValue(path);
		return unit.write(ac);
	}

	private String getDir(String path) {
		StringBuffer html = new StringBuffer(" ");
		String root = FileUtil.formatPath(Path.getVersionPath());
		String temp = path.substring(root.length());
		String directorys[] = "".equals(temp) ? null : StringUtil.split(temp,
				"/");
		if (!Checker.isEmpty(directorys)) {
			setDirectory(html, "root", root, false, true);
			html.append(" \\ ");
			for (int i = 0; i < directorys.length; i++)
				if (!"".equals(directorys[i])) {
					root = (new StringBuilder(String.valueOf(root))).append(
							directorys[i]).append("/").toString();
					if (i < directorys.length - 1) {
						setDirectory(html, directorys[i], root, false, true);
						html.append(" \\ ");
					} else {
						setDirectory(html, directorys[i], root, false, false);
					}
				}

		} else {
			setDirectory(html, "root", root, false, false);
		}
		return html.toString();
	}

	private String getList(String path) {
		StringBuffer html = new StringBuffer();
		String directorys[] = FileUtil.listDirectorys(path);
		if (!Checker.isEmpty(directorys)) {
			for (int i = 0; i < directorys.length; i++) {
				setDirectory(html, directorys[i], (new StringBuilder(String
						.valueOf(path))).append(directorys[i]).toString(),
						true, true);
				html.append("<br>");
			}

		}
		String files[] = FileUtil.listFiles(path);
		if (!Checker.isEmpty(files)) {
			for (int i = 0; i < files.length; i++) {
				setFile(html, files[i]);
				html.append("<br>");
			}

		}
		return html.toString();
	}

	private void setDirectory(StringBuffer html, String name, String value,
			boolean showIcon, boolean active) {
		html.append("<span>");
		if (showIcon)
			html.append("<img src=\"").append(InputUI.FOLDER_ICON)
					.append("\"/>");
		if (active)
			html.append(" <label onMouseOver=onOver() onMouseOut=onOut()")
					.append(" class=active onclick=setPath('").append(value)
					.append("')>").append(name).append("</label>");
		else
			html.append(" <label class=lable>").append(name).append("</label>");
		html.append("<span>");
	}

	private void setFile(StringBuffer html, String name) {
		html.append("<span><img src=\"").append(InputUI.FILE_ICON)
				.append("\"/> ").append(name).append("<span>");
	}
}
